package com.vlong.town.controller.admin.projectInfo;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.organization.Organization;
import com.vlong.town.entity.projectInfo.ProjectInfo;
import com.vlong.town.service.organization.OrganizationService;
import com.vlong.town.service.projectInfo.ProjectInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.file.FileUpload;
import com.vlong.town.util.pagination.PageTag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 旅游项目Controller
 * @author changxp
 * @version 2018-05-04
 */
@Controller
@RequestMapping("/projectInfo")
public class ProjectInfoController extends BaseController {


    @Autowired
    private ProjectInfoService projectInfoService;//旅游项目服务实现
    @Autowired
	private  OrganizationService  organizationService;

    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/list")
    public String list(
    		@RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="projectName") String projectName,
            @RequestParam(required=false, value="goodsId") Long goodsId
            //@RequestParam(required=false, value="status") Integer status,
            //@PathVariable( value="identify") Long identify
    		){


    	//查询商家信息
        List<Organization> orgList = organizationService.queryList(null, null, 1, 1, TownConstant.DEL_FLAG_0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
        request.setAttribute("orgList", orgList);
    	
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<ProjectInfo>  projectList = this.projectInfoService.queryList(null, null, null, goodsId, null, projectName, null, null, null, 2, null, null, null);
        PageInfo<ProjectInfo> pageInfo = new PageInfo<>(projectList);
        PageTag<ProjectInfo> pt = new PageTag<>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("projectName", projectName);
        request.setAttribute("goodsId", goodsId);
        request.setAttribute("identify", 1);//标识 1跳转到信息发布2跳转到信息审核
        //request.setAttribute("status", 2);
        for(ProjectInfo p : projectList) {
            p.setDesc(DataUtil.delHTMLTag(p.getDesc(), 0));
        }
        request.setAttribute("projectList", projectList);
    	return "admin/projectInfo/list_projectInfo";
    }
/**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/check")
    public String check(
    		@RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="projectName") String projectName,
            @RequestParam(required=false, value="goodsId") Long goodsId
    		){


    	//查询商家信息
        List<Organization> orgList = organizationService.queryList(null, null, 1, 1, TownConstant.DEL_FLAG_0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
        request.setAttribute("orgList", orgList);

        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<ProjectInfo>  projectList =new ArrayList<>();

        List<ProjectInfo> projectReject= this.projectInfoService.queryList(null, null, null, goodsId, null, projectName, null, null, null, 3, null, null, null);
        List<ProjectInfo>  projectCheck = this.projectInfoService.queryList(null, null, null, goodsId, null, projectName, null, null, null, 1, null, null, null);
        projectList.addAll(projectReject);
        projectList.addAll(projectCheck);
        PageInfo<ProjectInfo> pageInfo = new PageInfo<>(projectList);
        PageTag<ProjectInfo> pt = new PageTag<>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("projectName", projectName);
        request.setAttribute("goodsId", goodsId);
        //request.setAttribute("status", 1);
        request.setAttribute("identify", 2);//标识 1跳转到信息发布2跳转到信息审核
        //List<ProjectInfo> project  = new ArrayList<>();
        for(ProjectInfo p : projectList) {
            p.setDesc(DataUtil.delHTMLTag(p.getDesc(), 0));
            /*if(p.getStatus() !=2){
                project.add(p);
            }
            if(p.getStatus() ==2){
                project.add(p);
            }*/
        }
        request.setAttribute("projectList", projectList);
    	return "admin/projectInfo/list_projectInfo";
    }


    /**
     * 跳转新增页面
     * @return notice/add_notice
     */
    @GetMapping("/add")
    public String add() {
    	//查询商家信息
    	List<Organization> orgList = organizationService.queryList(null, null, 1, 1, TownConstant.DEL_FLAG_0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
    	request.setAttribute("goodsList", orgList);
        return "admin/projectInfo/add_projectInfo";
        /*if(marker == 1){
        }else {
            return "merchantProject/add_merchantProjectInfo";
        }*/

    }

    /**
     * 跳转编辑页面
     * @return notice/edit_notice
     */
    @GetMapping("/edit/{projectId}")
    public String editpage(@PathVariable(value="projectId") Long projectId,
                           @RequestParam(name="identify", required=false) Long identify) {
        ProjectInfo projectInfo = this.projectInfoService.queryById(projectId);
        List<Organization> orgList = organizationService.queryList(null, null, 1, 1, TownConstant.DEL_FLAG_0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);

    	request.setAttribute("orgList", orgList);
    	
    	List<String> pictureUrls = new ArrayList<>();
        String[] picture;
        if(projectInfo.getPicture()!=null&&!projectInfo.getPicture().equals("")){
        	picture=projectInfo.getPicture().split(",");
	        for(String p:picture){
                pictureUrls.add(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/" +"cdkeditor"+ "/" +p);
            }
	        request.setAttribute("pictureSize",picture.length);
        }
		request.setAttribute("pictureUrls",pictureUrls);
		
    	
    	request.setAttribute("projectInfo", projectInfo);
    	request.setAttribute("identify", identify);
    	//request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + noticeInfo.getThumbnail());
        return "admin/projectInfo/edit_projectInfo";
        /*if(identify == 1){
        }else {
            return "merchantProject/edit_merchantProjectInfo";
        }*/
    }

    /**
     * 添加旅游项目
     * @param goodsId 商家id orgId
     * @param goodsName 商家名称
     * @param name 名称
     * @param desc 介绍
     * @param price 价格
     * @param picture 图片
     * @return
     */
    @ResponseBody
    @PostMapping("addProject")
    public DataMessage add(
            @RequestParam(name="goodsId", required=false) Long goodsId,
            @RequestParam(name="goodsName", required=false) String goodsName,
            @RequestParam(name="name", required=false) String name,
            @RequestParam(name="desc", required=false) String desc,
            @RequestParam(name="price", required=false) Long price,
            @RequestParam(name="status", required=false) Integer status,
            @RequestParam(name="picture", required=false) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(name, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("项目名称不合法");
            return dm;
            
        }
        if(goodsId == null){
            goodsId = getOrgId();
            List<Organization> Organization =  organizationService.queryList(getOrgId(), null, 1, 1, TownConstant.DEL_FLAG_0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
            if(Organization.size()>0){
                goodsName = Organization.get(0).getOrgName();
            }
        }
        //User user = getLoginUser(request);
        //根据商家id查询商家信息
        this.projectInfoService.add(goodsId,1L, goodsId, goodsName, name, desc, price, picture,status, System.currentTimeMillis(), null, 0);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("旅游项目添加成功");
        return dm;
    }

    /**
     * 修改项目信息
     * @param desc 公告简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("editProject")
    public DataMessage editProject(
    		 @RequestParam(name="projectId", required=true) Long projectId,
    		 @RequestParam(name="goodsId", required=false) Long goodsId,
             @RequestParam(name="name", required=false) String name,
             @RequestParam(name="desc", required=false) String desc,
             @RequestParam(name="price", required=false) Long price,
             @RequestParam(name="picture", required=false) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(name, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("项目名称不合法");
            return dm;
        }
        //User user = getLoginUser(request);
        this.projectInfoService.edit(goodsId,null,projectId, goodsId, null, name, desc, price, picture, null, null, System.currentTimeMillis(), null,null);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("项目修改成功");
        return dm;
    }

    /**
     * 删除项目
     * @param projectId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/deleteProject")
    public DataMessage deleteUser(@RequestParam(required=true, value="projectId") Long projectId) {
        DataMessage dm = new DataMessage();
        if(projectId != null && projectId > 0L) {
            this.projectInfoService.delete(projectId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("项目删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除项目
     * @param projectIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="projectIds") String projectIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(projectIds, 1, 255)) {
            String[] projectIArray = projectIds.split(",");
            for(String projectId : projectIArray) {
            	 this.projectInfoService.delete(Long.parseLong(projectId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游项目删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    /**
     * 上传项目图片轮播图
     * @return DataMessage
     */
	@ResponseBody
	@RequestMapping(value = "/upload", method=RequestMethod.POST)
    public String upload(@RequestParam MultipartFile file) throws Exception{
        return FileUpload.upload(file);
    }

    /**
     * 跳转商家列表页面
     * @return
     */
    @RequestMapping("/merchantList")
    public String merchantList(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="projectName") String projectName,
            @RequestParam(required=false, value="status") Integer status
    ){

        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<ProjectInfo>  projectList = this.projectInfoService.queryList(getOrgId(), null, null, null, null, projectName, null, null, null, status, null, null, null);
        PageInfo<ProjectInfo> pageInfo = new PageInfo<>(projectList);
        PageTag<ProjectInfo> pt = new PageTag<>(request, "merchantList", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("projectName", projectName);
        request.setAttribute("status", status);
        for(ProjectInfo p : projectList) {
            p.setDesc(DataUtil.delHTMLTag(p.getDesc(), 0));
        }
        request.setAttribute("projectList", projectList);
        return "admin/merchantProject/list_merchantProjectInfo";
    }


    /**
     * 修改状态(通过,驳回)
     * @return notice/edit_notice
     */
    @ResponseBody
    @RequestMapping("/editStatus")
    public DataMessage editStatus(@RequestParam(required=false, value="projectId") Long projectId,
                                  @RequestParam(required=false, value="status") Integer status,
                                  @RequestParam(required=false, value="projectDesc") String projectDesc) {
        DataMessage dm = new DataMessage();
        this.projectInfoService.edit(null,null,projectId, null, null, null, null, null, null, status, null, System.currentTimeMillis(), null,projectDesc);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("修改成功");
        return dm;
    }

}
